﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Data.SqlClient;
using BUS;
using DTO;
using System.IO;
using System.Diagnostics;

namespace QLDT
{
    public partial class frm_DSSinhVien : DevExpress.XtraEditors.XtraForm
    {
        public bool TTThemSua = true;
        private string MaSV_cu;
        public frm_DSSinhVien()
        {
            InitializeComponent();
        }

        private void frm_DSSinhVien_Load(object sender, EventArgs e)
        {
            load();
        }
        public void loadDSKhoa()
        {
            gridlookKhoa.EditValue = null;
            gridlookKhoa.Properties.View.OptionsBehavior.AutoPopulateColumns = false;
            gridlookKhoa.Properties.AllowNullInput = DevExpress.Utils.DefaultBoolean.True;
            gridlookKhoa.Properties.ValueMember = "MaKhoa";
            gridlookKhoa.Properties.DisplayMember = "MaKhoa";
            gridlookKhoa.Properties.View.BestFitColumns();
            gridlookKhoa.Properties.PopupFormWidth = 400;
            gridlookKhoa.Properties.NullText = "Chọn khoa";
            gridlookKhoa.Properties.DataSource = BUS_Khoa.SelectKhoa();
        }
        public void loadDSKhoaHoc()
        {
            gridLookKhoaHoc.EditValue = null;
            gridLookKhoaHoc.Properties.View.OptionsBehavior.AutoPopulateColumns = false;
            gridLookKhoaHoc.Properties.AllowNullInput = DevExpress.Utils.DefaultBoolean.True;
            gridLookKhoaHoc.Properties.ValueMember = "MaKH";
            gridLookKhoaHoc.Properties.DisplayMember = "MaKH";
            gridLookKhoaHoc.Properties.View.BestFitColumns();
            gridLookKhoaHoc.Properties.PopupFormWidth = 400;
            gridLookKhoaHoc.Properties.NullText = "Chọn khóa học";
            gridLookKhoaHoc.Properties.DataSource = BUS_KhoaHoc.SelectKhoaHoc();
        }
        public void loadDSKhoaHoc_theokhoa(string makhoa)
        {
            gridLookKhoaHoc.EditValue = null;
            gridLookKhoaHoc.Properties.View.OptionsBehavior.AutoPopulateColumns = false;
            gridLookKhoaHoc.Properties.AllowNullInput = DevExpress.Utils.DefaultBoolean.True;
            gridLookKhoaHoc.Properties.ValueMember = "MaKH";
            gridLookKhoaHoc.Properties.DisplayMember = "MaKH";
            gridLookKhoaHoc.Properties.View.BestFitColumns();
            gridLookKhoaHoc.Properties.PopupFormWidth = 400;
            gridLookKhoaHoc.Properties.NullText = "Chọn khóa học";
            gridLookKhoaHoc.Properties.DataSource = BUS_KhoaHoc.SelectKhoaHoc_theokhoa(makhoa);
        }
        public void loadDSLop()
        {
            gridLookLop.EditValue = null;
            gridLookLop.Properties.View.OptionsBehavior.AutoPopulateColumns = false;
            gridLookLop.Properties.AllowNullInput = DevExpress.Utils.DefaultBoolean.True;
            gridLookLop.Properties.ValueMember = "Malop";
            gridLookLop.Properties.DisplayMember = "Malop";
            gridLookLop.Properties.View.BestFitColumns();
            gridLookLop.Properties.PopupFormWidth = 400;
            gridLookLop.Properties.NullText = "Chọn Lớp";
            gridLookLop.Properties.DataSource = BUS_Lop.SelectLop();
        }
        public void loadDSLop_theokhoahoc(string makh)
        {
            gridLookLop.EditValue = null;
            gridLookLop.Properties.View.OptionsBehavior.AutoPopulateColumns = false;
            gridLookLop.Properties.AllowNullInput = DevExpress.Utils.DefaultBoolean.True;
            gridLookLop.Properties.ValueMember = "Malop";
            gridLookLop.Properties.DisplayMember = "Malop";
            gridLookLop.Properties.View.BestFitColumns();
            gridLookLop.Properties.PopupFormWidth = 400;
            gridLookLop.Properties.NullText = "Chọn Lớp";
            gridLookLop.Properties.DataSource = BUS_Lop.SelectLop_theoKhoaHoc(makh);
        }
        public void loadDSLop_theokhoa(string makhoa)
        {
            gridLookLop.EditValue = null;
            gridLookLop.Properties.View.OptionsBehavior.AutoPopulateColumns = false;
            gridLookLop.Properties.AllowNullInput = DevExpress.Utils.DefaultBoolean.True;
            gridLookLop.Properties.ValueMember = "Malop";
            gridLookLop.Properties.DisplayMember = "Malop";
            gridLookLop.Properties.View.BestFitColumns();
            gridLookLop.Properties.PopupFormWidth = 400;
            gridLookLop.Properties.NullText = "Chọn Lớp";
            gridLookLop.Properties.DataSource = BUS_Lop.SelectLop_theoKhoa(makhoa);
        }
        private void load()
        {
            layoutMaLop_Txt.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
            layoutMaLop_lookup.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;

            gridDSSinhVien.DataSource = BUS_SinhVien.SelectSinhVien();
            txt_DSSVMaSV.Enabled = false;
            txt_DSSVTenSV.Enabled = false;
            datengaysinh.Enabled = false;
            txt_DSSVMaLop.Enabled = false;
            txt_DSSVDiaChi.Enabled = false;
            txt_DSSVEmail.Enabled = false;
            txt_DSSVSDT.Enabled = false;
            radioGroup_gioitinh.Enabled = false;
            gridlookKhoa.Enabled = true;
            gridLookKhoaHoc.Enabled = true;
            gridLookLop.Enabled = true;
            radio_DSSVDR.Enabled = true;
            gridDSSinhVien.Enabled = true;


            simpleBt_CapNhat.Enabled = simpleBt_Huy.Enabled = false;
            if (gv_DSSV.RowCount < 1)
            {
                simpleBt_Sua.Enabled = simpleBt_Xoa.Enabled = false;
            }
            else
                simpleBt_Sua.Enabled = simpleBt_Xoa.Enabled = true;
            simpleBt_Them.Enabled = true;
            Bindings();

            gridLookMaLop.Properties.View.OptionsBehavior.AutoPopulateColumns = false;
            gridLookMaLop.Properties.AllowNullInput = DevExpress.Utils.DefaultBoolean.True;
            gridLookMaLop.Properties.ValueMember = "Malop";
            gridLookMaLop.Properties.DisplayMember = "Malop";
            gridLookMaLop.Properties.View.BestFitColumns();
            gridLookMaLop.Properties.PopupFormWidth = 400;
            gridLookMaLop.Properties.NullText = "Chọn lớp";
            gridLookMaLop.Properties.DataSource = BUS_Lop.SelectLop();


            if (gv_DSSV.GetRowCellValue(gv_DSSV.FocusedRowHandle, "GioiTinh") == null)
            {
                return;
            }
            if (gv_DSSV.GetRowCellValue(gv_DSSV.FocusedRowHandle, "GioiTinh").ToString() == "True")
            {
                radioGroup_gioitinh.SelectedIndex = 0;
            }
            else
            {
                radioGroup_gioitinh.SelectedIndex = 1;
            }
            loadDSKhoa();
            loadDSKhoaHoc();
            loadDSLop();

        }

        private void Bindings()
        {
            txt_DSSVMaSV.DataBindings.Clear();
            txt_DSSVTenSV.DataBindings.Clear();
            //txt_DSSVGioiTinh.DataBindings.Clear();

            datengaysinh.DataBindings.Clear();
            txt_DSSVMaLop.DataBindings.Clear();
            txt_DSSVDiaChi.DataBindings.Clear();
            txt_DSSVEmail.DataBindings.Clear();
            txt_DSSVSDT.DataBindings.Clear();

            txt_DSSVMaSV.DataBindings.Add("Text", gridDSSinhVien.DataSource, "MaSV");
            txt_DSSVTenSV.DataBindings.Add("Text", gridDSSinhVien.DataSource, "TenSV");
            // txt_DSSVGioiTinh.DataBindings.Add("Text", gridDSSinhVien.DataSource, "GioiTinh");

            datengaysinh.DataBindings.Add("Text", gridDSSinhVien.DataSource, "NgaySinh");
            txt_DSSVMaLop.DataBindings.Add("Text", gridDSSinhVien.DataSource, "MaLop");
            txt_DSSVDiaChi.DataBindings.Add("Text", gridDSSinhVien.DataSource, "DiaChi");
            txt_DSSVEmail.DataBindings.Add("Text", gridDSSinhVien.DataSource, "Email");
            txt_DSSVSDT.DataBindings.Add("Text", gridDSSinhVien.DataSource, "SDT");

        }

        private void simpleBt_Xoa_Click(object sender, EventArgs e)
        {
            bool kq;
            if (MessageBox.Show("Bạn có chắc sẽ xóa sinh viên " + txt_DSSVMaSV.Text + " hay không, nếu xóa thì toàn bộ thông thông tin liên quan sẽ mất.", "Thông báo", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                kq = BUS_SinhVien.deleteSinhVientheoSV(txt_DSSVMaSV.Text);
                if (!kq)
                    XtraMessageBox.Show("Xóa thất bại", "Thông báo");
                else
                {
                    load();
                }
            }
            else
                load();

        }
        public void setenable(bool flag)
        {
            radioGroup_gioitinh.Enabled = flag;
            layoutMaLop_Txt.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
            layoutMaLop_lookup.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
            simpleBt_Them.Enabled = !flag;
            simpleBt_Sua.Enabled = !flag;
            simpleBt_Xoa.Enabled = !flag;
            simpleBt_CapNhat.Enabled = flag;
            simpleBt_Huy.Enabled = flag;
            gridlookKhoa.Enabled = !flag;
            gridLookKhoaHoc.Enabled = !flag;
            gridLookLop.Enabled = !flag;
            radio_DSSVDR.Enabled = !flag;
            gridDSSinhVien.Enabled = !flag;

            txt_DSSVMaSV.DataBindings.Clear();
            txt_DSSVTenSV.DataBindings.Clear();

            datengaysinh.DataBindings.Clear();
            txt_DSSVMaLop.DataBindings.Clear();
            txt_DSSVDiaChi.DataBindings.Clear();
            txt_DSSVEmail.DataBindings.Clear();
            txt_DSSVSDT.DataBindings.Clear();

            txt_DSSVMaSV.Text = "";
            txt_DSSVTenSV.Text = "";
            //txt_DSSVGioiTinh.Text = "";

            datengaysinh.Text = "";
            txt_DSSVMaLop.Text = "";
            txt_DSSVDiaChi.Text = "";
            txt_DSSVEmail.Text = "";
            txt_DSSVSDT.Text = "";

            txt_DSSVMaSV.Enabled = flag;
            txt_DSSVTenSV.Enabled = flag;
            //txt_DSSVGioiTinh.Enabled = true;

            datengaysinh.Enabled = flag;
            txt_DSSVMaLop.Enabled = flag;
            txt_DSSVDiaChi.Enabled = flag;
            txt_DSSVEmail.Enabled = flag;
            txt_DSSVSDT.Enabled = flag;
        }
        private void simpleBt_Them_Click(object sender, EventArgs e)
        {

            setenable(true);
            TTThemSua = true;

        }

        private void simpleBt_Huy_Click(object sender, EventArgs e)
        {
            load();
        }

        private void simpleBt_Sua_Click(object sender, EventArgs e)
        {
            radioGroup_gioitinh.Enabled = true;
            layoutMaLop_Txt.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
            layoutMaLop_lookup.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
            MaSV_cu = txt_DSSVMaSV.Text;
            simpleBt_Them.Enabled = false;
            simpleBt_Sua.Enabled = false;
            simpleBt_Xoa.Enabled = false;
            simpleBt_CapNhat.Enabled = true;
            simpleBt_Huy.Enabled = true;



            txt_DSSVMaSV.DataBindings.Clear();
            txt_DSSVTenSV.DataBindings.Clear();
            // txt_DSSVGioiTinh.DataBindings.Clear();

            datengaysinh.DataBindings.Clear();
            txt_DSSVMaLop.DataBindings.Clear();
            txt_DSSVDiaChi.DataBindings.Clear();
            txt_DSSVEmail.DataBindings.Clear();
            txt_DSSVSDT.DataBindings.Clear();

            txt_DSSVMaSV.Enabled = true;
            txt_DSSVTenSV.Enabled = true;
            // txt_DSSVGioiTinh.Enabled = true;

            datengaysinh.Enabled = true;
            txt_DSSVMaLop.Enabled = true;
            txt_DSSVDiaChi.Enabled = true;
            txt_DSSVEmail.Enabled = true;
            txt_DSSVSDT.Enabled = true;

            TTThemSua = false;


        }

        private void simpleBt_CapNhat_Click(object sender, EventArgs e)
        {
            if (!KTDulieu())
            {
                XtraMessageBox.Show("Nhập đầy đủ nga", "Thông báo");
                return;
            }
            DTO_SinhVien SV = new DTO_SinhVien();
            SV.MaSV = txt_DSSVMaSV.Text;
            SV.TenSV = txt_DSSVTenSV.Text;
            SV.DiaChi = txt_DSSVDiaChi.Text;
            SV.NgaySinh = DateTime.Parse(datengaysinh.Text);
            if (radioGroup_gioitinh.SelectedIndex == 0)
                SV.GioiTinh = true;
            else
                SV.GioiTinh = false;
            SV.MaLop = gridLookMaLop.EditValue.ToString();
            SV.Email = txt_DSSVEmail.Text;
            SV.SDT = txt_DSSVSDT.Text;

            if (TTThemSua)
            {

                if (BUS_SinhVien.insertSinhVien(SV))
                    load();
                else
                    XtraMessageBox.Show("lỗi từ hệ thống, hãy liên hệ với bộ phận kỹ thuật, mã lỗi [simpleBt_CapNhat_Click]", "Thông báo");
            }
            else
            {
                if (BUS_SinhVien.updateSinhVien(SV, MaSV_cu))
                    load();
                else
                    XtraMessageBox.Show("lỗi từ hệ thống, hãy liên hệ với bộ phận kỹ thuật, mã lỗi [simpleBt_CapNhat_Click]", "Thông báo");
            }

        }

        private bool KTDulieu()
        {
            if (txt_DSSVMaSV.Text == "")
                return false;
            if (txt_DSSVTenSV.Text == "")
                return false;
            if (datengaysinh.Text == "")
                return false;
            if (gridLookMaLop.EditValue == null)
                return false;
            if (txt_DSSVDiaChi.Text == "")
                return false;
            if (txt_DSSVEmail.Text == "")
                return false;
            if (txt_DSSVSDT.Text == "")
                return false;
            return true;

        }

        private void txt_DSSVMaSV_EditValueChanged(object sender, EventArgs e)
        {
            if (txt_DSSVMaSV.Enabled == true)
                return;
            if (gv_DSSV.GetRowCellValue(gv_DSSV.FocusedRowHandle, "GioiTinh") == null)
            {
                return;
            }
            if (gv_DSSV.GetRowCellValue(gv_DSSV.FocusedRowHandle, "GioiTinh").ToString() == "True")
                radioGroup_gioitinh.SelectedIndex = 0;
            else
                radioGroup_gioitinh.SelectedIndex = 1;

        }

        private void txt_DSSVMaSV_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                txt_DSSVTenSV.Focus();
        }

        private void txt_DSSVTenSV_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                datengaysinh.Focus();
        }

        private void datengaysinh_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                radioGroup_gioitinh.Focus();
        }

        private void radioGroup_gioitinh_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                txt_DSSVMaLop.Focus();
        }

        private void txt_DSSVMaLop_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                txt_DSSVDiaChi.Focus();

        }

        private void txt_DSSVDiaChi_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                txt_DSSVEmail.Focus();
        }

        private void txt_DSSVEmail_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                txt_DSSVSDT.Focus();
        }

        private void txt_DSSVSDT_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                simpleBt_CapNhat.Focus();
        }

        private void txt_DSSVSDT_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar)
            && !char.IsDigit(e.KeyChar)
            && e.KeyChar != '.')
            {
                e.Handled = true;
            }


            if (e.KeyChar == '.'
                && (sender as TextBox).Text.IndexOf('.') > -1)
            {
                e.Handled = true;
            }



        }

        private void radio_DSSVDR_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (radio_DSSVDR.SelectedIndex == 0)
            {
                gridDSSinhVien.DataSource = BUS_SinhVien.SelectSinhVien();
            }
            else if (radio_DSSVDR.SelectedIndex == 1)
            {
                gridDSSinhVien.DataSource = BUS_SinhVien.SelectSinhVien_Dau();
            }
            else if (radio_DSSVDR.SelectedIndex == 2)
            {
                gridDSSinhVien.DataSource = BUS_SinhVien.SelectSinhVien_Rot();
            }
            Bindings();
        }

        private void gridLookKhoaHoc_EditValueChanged(object sender, EventArgs e)
        {
            if (gridLookKhoaHoc.EditValue == null)
            {
                return;
            }
            string makh = gridLookKhoaHoc.EditValue.ToString();
            List<DTO_SinhVien> listSinhVien = BUS_SinhVien.SelectSinhVienTheoKhoaHoc(makh);
            gridDSSinhVien.DataSource = BUS_SinhVien.SelectSinhVienTheoKhoaHoc(makh);
            loadDSLop_theokhoahoc(makh);
            Bindings();
        }

        private void gridlookKhoa_EditValueChanged(object sender, EventArgs e)
        {
            if (gridlookKhoa.EditValue == null)
            {
                return;
            }
            string makhoa = gridlookKhoa.EditValue.ToString();
            List<DTO_SinhVien> listSinhVien = BUS_SinhVien.SelectSinhVienTheoKhoa(makhoa);
            gridDSSinhVien.DataSource = BUS_SinhVien.SelectSinhVienTheoKhoa(makhoa);
            loadDSKhoaHoc_theokhoa(makhoa);
            loadDSLop_theokhoa(makhoa);
            Bindings();
        }

        private void gridLookLop_EditValueChanged(object sender, EventArgs e)
        { 
            if(gridLookLop.EditValue==null)
            {
                return;
            }
            string malop = gridLookLop.EditValue.ToString();
            List<DTO_SinhVien> listSinhVien = BUS_SinhVien.SelectSinhVienTheoLop(malop);
            gridDSSinhVien.DataSource = BUS_SinhVien.SelectSinhVienTheoLop(malop);
            Bindings();
        }

        private void simpleBt_DSExcel_Click(object sender, EventArgs e)
        {


        }

        private void fileSystemWatcher1_Changed(object sender, FileSystemEventArgs e)
        {

        }
    }

}